package com.sservice.commonBusiness.service.impl;

import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import com.sservice.commonBusiness.e.File;
import com.sservice.commonBusiness.mapper.FileMapper;
import com.sservice.commonBusiness.service.FileService;
import com.sservice.commonBusiness.vo.FileVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by byte_su@163.com on 2018/01/12.
 */
@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private FileMapper fileMapper;

    @Override
    public List<FileVo> selectByDataR(FileVo record) {
        return fileMapper.selectByDataR(record);
    }

    @Override
    public File saveFileInfo(String name, String desc1, String ownerId, Integer from, Integer type,
                             String url, String extension, String originName, long size, Integer folderId) {
        return saveFileInfo(name, desc1, ownerId, from, type, url, extension, originName, size, null);
    }

    public File saveFileInfo(
            String name,
            String desc1,
            String ownerId,
            Integer from,
            Integer type,
            String url,
            String extension,
            String originName,
            long size,
            Integer folderId,
            Integer sysId
    ) {
        File file = new File();
        file.setName(name);
        file.setDesc1(desc1);
        file.setOwnerId(ownerId == null ? "1" : ownerId); // 默认后台管理员
        file.setFrom1(from);
        file.setFolderId(folderId);
        file.setType1(type);
        file.setUrl(url);
        file.setExtension(extension);
        file.setOriginName(originName);
        file.setSize(size);
        file.setSysId(sysId);
        fileMapper.insertSelective(file);
        return file;
    }


    @Override
    public int deleteByPrimaryKey(Integer id) {
        return fileMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(File record) {
        return fileMapper.insert(record);
    }

    @Override
    public int insertSelective(File record) {
        return fileMapper.insertSelective(record);
    }

    @Override
    public File selectByPrimaryKey(Integer id) {
        return fileMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(File record) {
        return fileMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(File record) {
        return fileMapper.updateByPrimaryKey(record);
    }

    /**
     * 分页
     *
     * @param record
     * @return
     */
    @Override
    public ListResult<FileVo> page(FileVo record) {
        Pagination pagination = new Pagination(record.getPage(), fileMapper.count(record), (int) record.getSize());
        ListResult lr = new ListResult(true, fileMapper.queryByPage(record));
        lr.setPage(pagination);
        return lr;
    }

    @Override
    public List<File> getByUrls(List<String> record) {
        return fileMapper.selectByUrls(record);
    }
}
